package com.tool_stdy.thread;

/**
 * Created by yan_li on 2016/1/10.
 */
public class ThreadInterrupt {
    public static void main(String[] args) throws InterruptedException {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
       // t1.start();
        t2.start();
        Thread.sleep(200);
        System.out.println("t1被中断");
       // t1.interrupt();
        t2.join();
      //  Thread.sleep(2000000);
        System.out.println("Over");
    }
}
class Thread1 extends Thread{

    @Override
    public void run() {
        int i=0;
        while(true){
            System.out.println(Thread.currentThread().isInterrupted());
            if(!Thread.currentThread().isInterrupted()){
                System.out.println("线程:"+Thread.currentThread().getName()+"run..."+i);
                i++;
            }else{
                System.out.println(Thread.currentThread().getName()+"--EXIT");
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println("线程被中断.."+Thread.currentThread().getName());
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }
}
class Thread2 extends Thread{

    @Override
    public void run() {
        int i=0;
        while(true){
            if(!Thread.currentThread().isInterrupted()){
                i++;
                if(i>10){
                    break;
                }
                System.out.println("线程"+Thread.currentThread().getName()+"run.."+i);
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                System.out.println("线程被中断.."+Thread.currentThread().getName());
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }
}